package com.zkzx.operation.api.service.menu;

import com.zkzx.common.global.Global;
import com.zkzx.operation.api.controller.menu.vo.MenuVO;
import com.zkzx.operation.api.mapper.AuthorityMapper;
import com.zkzx.operation.api.mapper.entity.Authority;
import com.zkzx.operation.api.mapper.entity.RoleAuthority;
import com.zkzx.operation.api.mapper.entity.UserRole;
import com.zkzx.operation.api.service.authorization.AuthorityService;
import com.zkzx.operation.api.service.authorization.RoleAuthorityService;
import com.zkzx.operation.api.service.authorization.UserRoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author young Email:yanghui@zkraising.com
 * @description
 * @company 北京中科智兴物联科技有限公司 @版权所有
 * @since 2021年04月27日 16时17分
 */
@Slf4j
@Service
public class MenuService {

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private RoleAuthorityService roleAuthorityService;

    @Autowired
    private AuthorityService authorityService;

    @Resource
    private AuthorityMapper authorityMapper;

    /**
     * 获取用户对应的栏木树
     * @param userId 用户id
     * @return 栏木树
     */
    public List<MenuVO> findMenusByUserId(Integer userId){

        List<MenuVO> menuVOS = new ArrayList<>();

        // 获取用户管理的角色信息
        UserRole userRole = userRoleService.getByUserId(userId);
        if(Objects.isNull(userRole)){
            log.error("当前用户未分配角色信息:{}",userId);
            return menuVOS;
        }


        // 获取角色对应得到一级菜单栏目
        List<RoleAuthority> roleAuthorities = roleAuthorityService.getByRoleIdAndLevelAndIsMenu(userRole.getRoleid(), Global.ONE,Boolean.TRUE);

        if (CollectionUtils.isEmpty(roleAuthorities)){
            log.error("当前角色未关联菜单:{}",userRole.getRoleid());
            return menuVOS;
        }

        // 遍历菜单 需求确定只会有二级菜单 暂时写死
        roleAuthorities.stream().map(roleAuthority -> authorityMapper.selectByPrimaryKey(roleAuthority.getAuthorityid())).forEach(authority -> {
            List<MenuVO> childMenus = new ArrayList<>();
            List<Authority> authorities = authorityService.getByParentIdAndIsMenu(authority.getId(), Boolean.TRUE);

            // 加载二级菜单
            if (CollectionUtils.isNotEmpty(authorities)) {
                List<MenuVO> list = new ArrayList<>();
                for (Authority authority1 : authorities) {
                    // 校验权限是否匹配
                    if(!roleAuthorityService.checkRoleAuthority(userRole.getRoleid(),authority1.getId())){
                        continue;
                    }
                    MenuVO build = MenuVO.builder()
                            .id(authority1.getId())
                            .name(authority1.getName())
                            .code(authority1.getCode())
                            .path(authority1.getPath())
                            .method(authority1.getMethod())
                            .parentId(authority1.getParentid())
                            .level(authority1.getLevel())
                            .icon(authority1.getIcon())
                            .childMenus(Collections.emptyList())
                            .build();
                    list.add(build);
                }
                childMenus = list;

            }
            // 封装栏木树信息
            menuVOS.add( MenuVO.builder()
                    .id(authority.getId())
                    .name(authority.getName())
                    .code(authority.getCode())
                    .path(authority.getPath())
                    .method(authority.getMethod())
                    .parentId(authority.getParentid())
                    .level(authority.getLevel())
                    .icon(authority.getIcon())
                    .childMenus(childMenus)
                    .build());
        });

        return menuVOS;
    }
}
